#include "../YMCV/CORE/YMCV_KmeansPlusPlus.h"

void runYMCV_Demo(CVIMAGE myImage0)
{
    int stx = 20,sty = 40;
    /*-------------------------- kmeans数据分类测试 --------------------------------------------------*/
    CVIMAGE myImagek = YMCV_Creat_Img_Creat(200, 150, CVGrayType);
	  LCD_Clear(WHITE);
	  YMCV_Image_FullFill(myImagek,(CVcolor){.gray=128});//填充底色为白色
    LCD_show_image(stx + 0, sty + 0, myImagek);//绘图背景刷新

    YMCV_Srand(99);//初始化随机数种子
    
#define dataN 2000 //数据量2000个

    //数据标签，需要和待分类数据个数相同
    uint8 labels[dataN] = {0,1,2,4,4,2,2,4,4,4,4,4,1,1,4,3,0,4,2,0,2,3,4,2,3,3,2,4,4,2,0,3,3,1,4,3,1,1,4,1,1,2,3,2,4,0,3,0,2,3,1,0,4,4,2,0,2,1,3,1,1,2,2,2,3,3,2,1,0,0,0,0,2,0,2,1,4,4,4,1,2,1,4,2,2,2,0,4,0,2,0,1,2,3,1,0,1,1,1,2,1,2,3,4,1,4,4,0,4,0,0,1,2,4,1,0,4,4,0,4,2,0,4,0,1,1,2,3,1,4,3,0,4,1,1,1,4,2,3,0,1,4,1,2,4,4,3,1,1,3,2,4,3,1,3,4,2,0,0,4,0,1,4,2,0,3,4,4,4,0,4,3,2,4,2,1,4,0,4,0,0,0,1,1,1,2,0,4,0,4,1,1,0,2,3,1,4,1,3,1,3,3,3,3,2,1,0,4,4,4,0,3,0,0,4,2,4,0,1,1,0,2,4,1,4,0,2,0,0,1,4,3,1,1,0,1,2,2,4,4,0,0,3,0,4,3,1,1,4,1,4,3,4,4,4,0,2,3,0,0,0,3,3,0,1,4,2,3,2,1,0,4,1,3,0,0,3,2,1,1,2,0,4,3,2,2,2,1,4,0,4,1,0,4,1,3,1,0,1,1,4,0,4,4,2,1,3,4,4,1,0,1,4,2,4,4,1,0,2,4,2,0,2,0,0,4,0,4,1,0,2,1,2,1,1,0,0,1,2,3,0,4,3,4,2,2,3,1,1,0,4,0,1,2,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2,2,2,4,2,0,1,2,2,4,0,4,1,3,1,4,1,4,1,4,4,3,4,0,4,0,0,1,0,0,4,2,0,0,0,1,1,2,4,1,4,0,4,3,1,0,4,4,1,1,1,4,0,0,3,3,1,1,0,3,4,2,3,3,1,2,3,1,0,0,4,0,4,3,4,4,3,2,4,4,4,1,3,0,4,1,4,4,0,4,4,4,0,2,2,1,0,3,2,1,4,2,2,1,1,3,0,4,4,4,};

    CVVECTORS_F32 myvec = YMCV_Creat_Vector_F32(2, dataN);//创建向量

    for (int i = 0; i < myvec->dataNum; i++)//初始化向量值
    {
        float32* thisvec = myvec->datap + i * myvec->dim;
        thisvec[0] = YMCV_Randint() % myImagek->width;//宽
        thisvec[1] = YMCV_Randint() % myImagek->height;//高
    }

    uint8 Knum = 5;//k=5个中心
    CVVECTORS_F32 center = YMCV_Creat_Vector_F32(2, Knum);//创建k个中心,向量维度与数据相同

    //Kmeans数据中心提取
    YMCV_FVector_Kmeans_GetCenter(myvec, labels, center, 100); //带标签的数据，会自动更新不正确的标签
    
		////不带标签的数据
		//YMCV_FVector_Kmeans_GetCenter(myvec, NULL, center, 100); 
    ////进行Kmeans数据分类，获得标签
    //YMCV_FVector_Kmeans_Classify(myvec, center, labels); //输出数据标签类别

    //绘制数据
    for (int i = 0; i < myvec->dataNum; i++)
    {
        float32* thisvec = myvec->datap + i * myvec->dim;
        int cl = 255 * labels[i] / (Knum - 1);//类别转颜色索引

        //绘制数据小矩形，颜色从热力表中拿
        LCD_Fill_ConstRgbRect(stx + thisvec[0] - 1, sty + thisvec[1] - 1, 2, 2, YMCV_HeatPalette[cl]);
    }
    //绘制中心点
    for (int i = 0; i < center->dataNum; i++)
    {
        float32* thisvec = center->datap + i * center->dim;
        int cl = 255 * i / (Knum - 1);//类别转颜色索引

        //绘制数据大矩形，颜色从热力表中拿
        LCD_Fill_ConstRgbRect(stx + thisvec[0] - 2, sty + thisvec[1] - 2, 5, 5, YMCV_HeatPalette[cl]);
    }

    Delay(300);
    YMCV_Free_Vector_F32_Free(myvec);
    YMCV_Free_Vector_F32_Free(center);
    YMCV_Free_ImgFree(myImagek);
}